#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

int main(void)
{
    CURL *curl;
    CURLcode res;
    char *url = "http://www.example.com";
    char *html = NULL;
    long html_size = 0;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        } else {
            html_size = strlen(html);
            printf("HTML size: %ld\n", html_size);
            printf("HTML content:\n%s\n", html);
        }
        curl_easy_cleanup(curl);
    }

    free(html);
    return 0;
}

size_t write_callback(char *ptr, size_t size, size_t nmemb, char **userdata)
{
    size_t realsize = size * nmemb;
    char *temp = realloc(*userdata, strlen(*userdata) + realsize + 1);
    if (temp == NULL) {
        fprintf(stderr, "realloc() failed\n");
        return 0;
    }
    *userdata = temp;
    memcpy(&((*userdata)[strlen(*userdata)]), ptr, realsize);
    (*userdata)[strlen(*userdata) + realsize] = '\0';
    return realsize;
}

